package game;

import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;

public class PointPlatform extends Platform {
    public static final long serialVersionUID = 1L;
    protected int moveSpeed;
    protected int spot;
    protected ArrayList<Point> points = new ArrayList<Point>();
    protected ArrayList<Point> path = new ArrayList<Point>();

    public PointPlatform(){}

    public PointPlatform(Level g, int x, int y, int w, int h, Color c, int speedMultiplier){
        super(g, x, y, w, h, c);
        moveSpeed = speedMultiplier;
        spot = 0;
        addPoint(x,y);
    }

    public void addPoint(int x, int y){
        points.add(new Point(x,y));
    }

    public void adjust(int x1, int y1, int x2, int y2) {
        if ((x1 - x2) > 0) {
            adjust(x2, y2, x1, y1);
            return;
        }
        if (Math.abs(y2 - y1) > Math.abs(x2 - x1)) {
            swap(y1, x1, y2, x2);
            return; 
        }
        calculatePath(x1, y1, x2, y2, false);
    }

    public void swap(int x3, int y3, int x4, int y4) {
        if ((x3 - x4) > 0) {
            swap(x4, y4, x3, y3);
            return;
        }
        calculatePath(x3, y3, x4, y4, true);
    }
    
    public void calculatePath(int x1, int y1, int x2, int y2, boolean invert){
        int x = x1, y = y1, sum = x2 - x1, Dx = 2 * (x2 - x1), Dy = Math.abs(2 * (y2 - y1));
        int prirastokDy = ((y2 - y1) > 0) ? 1 : -1;
        for (int i = 0; i <= x2 - x1; i++) {
            if(invert)
                path.add(new Point(y, x));
            else
                path.add(new Point(x, y));
            x++;
            sum -= Dy;
            if (sum < 0){
                y = y + prirastokDy;
                sum += Dx;
            }
        }
    }
    
    public void invertPath(){
        ArrayList<Point> temp = new ArrayList<Point>();
        for(int a = path.size() - 1; a >= 0; a--){
            temp.add(path.get(a));
        }
        path = temp;
    }
    
    public void updatePath(int x2, int y2){
        path.clear();
        adjust(this.x, this.y, x2, y2);
        if(path.get(0).x == x2 && path.get(0).y == y2)
            invertPath();
        int counter = 0;
        int speed = path.size() / moveSpeed;
        for(int a = 1; a < path.size() - 1; a++){
            counter++;
            if(counter == speed){
                counter = 0;
                path.remove(a);
            }
        }
    }

    @Override
    public void update(){
    	super.update();
        if(points.size() < 1){
            return;
        }else{
            if(points.get(spot).x == x && points.get(spot).y == y){
                spot++;
                if(points.size() == spot){
                    spot = 0;
                }
                updatePath(points.get(spot).x, points.get(spot).y);
            }else{
                xMotion = path.get(0).x - x;
                downMotion = path.get(0).y - y;
                x += (int) xMotion;
                y += (int) downMotion;
                path.remove(0);
            }
        }
    }
}